<?php

	echo '<?xml version="1.0" encoding="UTF-8"?>
';

?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
	<dict>
		<key>PayloadContent</key>
		<array>
			<dict>
				<key>PayloadDescription</key>
				<string></string>
				<key>PayloadDisplayName</key>
				<string>Restrictions</string>
				<key>PayloadIdentifier</key>
				<string>com.google.code.airobot-ng.restrictions1</string>
				<key>PayloadOrganization</key>
				<string>Airobot-ng</string>
				<key>PayloadType</key>
				<string>com.apple.applicationaccess</string>
				<key>PayloadUUID</key>
				<string>53BA7C55-E7D3-6531-B27A-53A63F733AFE</string>
				<key>PayloadVersion</key>
				<integer>1</integer>
				<key>allowAddingGameCenterFriends</key>
				<true/>
				<key>allowAppInstallation</key>
				<true/>
				<key>allowAssistant</key>
				<true/>
				<key>allowAssistantWhileLocked</key>
				<true/>
				<key>allowBookstoreErotica</key>
				<true/>
				<key>allowCamera</key>
				<true/>
				<key>allowCloudBackup</key>
				<true/>
				<key>allowCloudDocumentSync</key>
				<true/>
				<key>allowDiagnosticSubmission</key>
				<true/>
				<key>allowExplicitContent</key>
				<true/>
				<key>allowGlobalBackgroundFetchWhenRoaming</key>
				<true/>
				<key>allowInAppPurchases</key>
				<true/>
				<key>allowMultiplayerGaming</key>
				<true/>
				<key>allowPassbookWhileLocked</key>
				<true/>
				<key>allowPhotoStream</key>
				<true/>
				<key>allowSafari</key>
				<true/>
				<key>allowScreenShot</key>
				<true/>
				<key>allowSharedStream</key>
				<true/>
				<key>allowUntrustedTLSPrompt</key>
				<true/>
				<key>allowVideoConferencing</key>
				<true/>
				<key>allowVoiceDialing</key>
				<true/>
				<key>allowYouTube</key>
				<true/>
				<key>allowiTunes</key>
				<true/>
				<key>forceEncryptedBackup</key>
				<false/>
				<key>forceITunesStorePasswordEntry</key>
				<false/>
				<key>ratingApps</key>
				<integer>1000</integer>
				<key>ratingMovies</key>
				<integer>1000</integer>
				<key>ratingRegion</key>
				<string>fr</string>
				<key>ratingTVShows</key>
				<integer>1000</integer>
				<key>safariAcceptCookies</key>
				<integer>2</integer>
				<key>safariAllowAutoFill</key>
				<true/>
				<key>safariAllowJavaScript</key>
				<true/>
				<key>safariAllowPopups</key>
				<true/>
				<key>safariForceFraudWarning</key>
				<false/>
			</dict>
<?php

	$credential_input_map = array();
	$credential_output_list = array();
	$credential_output_map = array();

	foreach (array_map('trim', file($_SERVER['argv'][1])) as $line)
	{
		if (1 === preg_match('/ "([^"]+)" "([0-9A-F]+)"$/', $line, $match))
		{
			$credential_output_map[$match[1]][$match[2]] = true;
			$credential_input_map[$line] = $line;
		}
	}

	usort($credential_input_map, 'strnatcasecmp');
	file_put_contents($_SERVER['argv'][1], implode(PHP_EOL, $credential_input_map).PHP_EOL);

	$index = 1;
	uksort($credential_output_map, 'strnatcasecmp');
	foreach ($credential_output_map as $essid => $password_map)
	{
		ksort($password_map);
		foreach (array_keys($password_map) as $password)
		{
			$credential_output_list[] = '"'.$essid.'" "'.$password.'"';

			++$index;
			$begin =
'			<dict>
				<key>AutoJoin</key>
				<true/>
				<key>EncryptionType</key>
				<string>Any</string>
				<key>HIDDEN_NETWORK</key>
				<false/>
				<key>Password</key>
				<string>'.$password.'</string>
				<key>PayloadDescription</key>
				<string>Configures wireless connectivity settings.</string>
				<key>PayloadDisplayName</key>
				<string>WiFi ('.$essid.')</string>
				<key>PayloadIdentifier</key>
				<string>com.google.code.airobot-ng.wifi';
			$middle =
				'</string>
				<key>PayloadOrganization</key>
				<string>Airobot-ng</string>
				<key>PayloadType</key>
				<string>com.apple.wifi.managed</string>
				<key>PayloadUUID</key>
				<string>';
			$end =
				'</string>
				<key>PayloadVersion</key>
				<integer>1</integer>
				<key>ProxyType</key>
				<string>None</string>
				<key>SSID_STR</key>
				<string>'.$essid.'</string>
			</dict>
';
			$hash = strtoupper(sha1($begin.$middle.$end));
			echo $begin, $index, $middle, substr($hash, 0, 8), '-', substr($hash, 8, 4), '-', substr($hash, 12, 4), '-', substr($hash, 16, 4), '-', substr($hash, 20, 12), $end;
		}
	}

	file_put_contents($_SERVER['argv'][2], implode(PHP_EOL, $credential_output_list));

?>
		</array>
		<key>PayloadDescription</key>
		<string>Wi-Fi credentials from Airobot-ng</string>
		<key>PayloadDisplayName</key>
		<string>Wi-Fi</string>
		<key>PayloadIdentifier</key>
		<string>com.google.code.airobot-ng</string>
		<key>PayloadOrganization</key>
		<string>Airobot-ng</string>
		<key>PayloadRemovalDisallowed</key>
		<false/>
		<key>PayloadType</key>
		<string>Configuration</string>
		<key>PayloadUUID</key>
		<string>C7B453DE-FD61-8E0D-F909-E91380B1029D</string>
		<key>PayloadVersion</key>
		<integer>1</integer>
	</dict>
</plist>

